package net.xju.dao;

import net.xju.common.utils.JdbcUtilsV1;
import net.xju.common.utils.PageInfo;
import net.xju.entity.User;

import java.sql.Connection;
import java.util.List;

public class UserDao {

    //登录
    public User login(String username, String password , String role) {
        Connection conn = JdbcUtilsV1.getConnection();
        String sql = "select id,role,username,email,gender,avatar,create_time as createTime,update_time as updateTime,deleted from user where deleted = 0 and username = ? and password = ?";
       System.out.println(sql);
        return JdbcUtilsV1.executeQueryOne(conn,User.class, sql, username, password);
    }
//添加
    public void insert(User user) {
        String sql = "insert into user(role,username,password,email,gender,avatar,create_time,update_time,deleted) values(?,?,?,?,?,?,?,?,?)";
        JdbcUtilsV1.executeUpdate(sql, user.getRole(),user.getUsername(),user.getPassword(),
                user.getEmail(), user.getGender(), user.getAvatar(), user.getCreateTime(), user.getUpdateTime(),0);
    }

    public User selectOne(int id) {
        Connection conn = JdbcUtilsV1.getConnection();
        String sql = "select id,role,username,email,gender,avatar," +
                "create_time as createTime,update_time as updateTime," +
                "deleted from user where deleted = 0 and id = ?";
        return JdbcUtilsV1.executeQueryOne(conn, User.class,sql, id);
    }
//删除
    public void delete(int id) {
        String sql = "update user set deleted = 1 where id = ?";
        JdbcUtilsV1.executeUpdate(sql, id);
    }
//修改
    public void update(User user) {
        String sql = "update user set ";
        boolean isFirst = true; // 标志变量，用于判断是否是第一个字段

        if (user.getUsername() != null && !user.getUsername().isEmpty()) {
            sql += "username = '" + user.getUsername() + "'";
            isFirst = false;
        }
        if (user.getPassword() != null && !user.getPassword().isEmpty()) {
            if (!isFirst) {
                sql += ", ";
            }
            sql += "password = '" + user.getPassword() + "'";
            isFirst = false;
        }
        if (user.getEmail() != null && !user.getEmail().isEmpty()) {
            if (!isFirst) {
                sql += ", ";
            }
            sql += "email = '" + user.getEmail() + "'";
            isFirst = false;
        }
        if (user.getGender() != null && !user.getGender().isEmpty()) {
            if (!isFirst) {
                sql += ", ";
            }
            sql += "gender = '" + user.getGender() + "'";
            isFirst = false;
        }
        if (user.getAvatar() != null && !user.getAvatar().isEmpty()) {
            if (!isFirst) {
                sql += ", ";
            }
            sql += "avatar = '" + user.getAvatar() + "'";
            isFirst = false;
        }
        sql += ", update_time = '" + user.getUpdateTime() + "' where id = " + user.getId();
        JdbcUtilsV1.executeUpdate(sql);
    }
//分页查询
    public PageInfo<User> pageList(String username, String gender, int pageNum, int pageSize) {

        Connection conn = JdbcUtilsV1.getConnection();
        String sql = "select id,role,username,email,gender,avatar," +
                "create_time as createTime,update_time as updateTime," +
                "deleted from user where deleted = 0";
        if (username != null && !username.isEmpty()){
            sql += " and username like '%" + username + "%'";
        }
        if (gender != null && !gender.isEmpty()){
            sql += " and gender = '" + gender + "'";
        }
        sql += " order by id desc";
        sql += " limit " + (pageNum - 1) * pageSize + "," + pageSize;
        List<User> users = JdbcUtilsV1.executeQuery(conn, User.class, sql);

        String countSql = "select count(*) from user where deleted = 0";
        long count = JdbcUtilsV1.executeQueryOneValue(countSql);

        PageInfo<User> pageInfo = new PageInfo<>(pageNum, pageSize, (int)count, users);
        return pageInfo;
    }

//    public User login(String username, String password, String role) {
//        Connection conn = JdbcUtilsV1.getConnection();
//        String table = null;
//        if(role.equals("admin")){
//            table = "admins";
//        } else if (role.equals("shangjia")) {
//            table = "shangjia";
//        } else if (role.equals("yonghu")) {
//            table = "yonghu";
//        }
//        String sql = "select id,username,pwd,role from "+table+" where username = ? and pwd = ?";
//
//        System.out.println(JdbcUtilsV1.executeQueryOne(conn,CarUser.class, sql, username, password));
//
//         return JdbcUtilsV1.executeQueryOne(conn,User.class, sql, username, password);
//    }
}
